home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Symantec Versions / C03 Sound Recording / P03 Sound Save / SoundSave.c next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  4.5 KB  |  185 lines  |  [TEXT/KAHL]

  1. //____________________________________________________________
  2. //    SoundSave.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8. //
  9. //    The "Graphics and Sound Programming for the Mac" book shows the variable theSound 
  10. //    having a data type of SndListHandle. The book does this in preparation for the call
  11. //    to SndRecord(). The book shows that the third parmater of this function as a 
  12. //    SndListHandle - as expected by the version of the Sound.h universal header file used
  13. //    by Metrowerks at the time of this writing. Symantec uses a different version of 
  14. //    Sound.h. If Symantec updates the Apple Universal Header files, you may get a compilation 
  15. //    error. If you do, it will concern code in the PlaySoundResourceSynch() function.
  16. //    Here are the changes you'll need to make. Change the code to match the code in the book.
  17.  
  18.  
  19.  
  20. //____________________________________________________________
  21.  
  22. #include <Sound.h>
  23. #include <SoundInput.h>
  24.  
  25.  
  26. //____________________________________________________________
  27.  
  28. void     InitializeToolbox( void );
  29. Boolean  IsSoundInputAvailable( void );
  30. OSErr    RecordSoundToMemory( Handle * );
  31. long     SaveSoundFromMemoryToResource( Handle );
  32. OSErr    PlaySoundResourceSynch( short );
  33.  
  34.  
  35. //____________________________________________________________
  36.  
  37. #define    kHeapReserve              75 * 1024
  38. #define    kSndResIDMaxReserved      8191
  39.  
  40.  
  41. //____________________________________________________________
  42.  
  43. void  main( void )
  44. {
  45.    NumVersion  theSndMgrVers;
  46.    OSErr       theError;
  47.    Handle      theSound;
  48.    long        theNewSoundID;
  49.    Boolean     soundInputPresent;   
  50.    
  51.    InitializeToolbox();
  52.    
  53.    MaxApplZone();
  54.  
  55.    theSndMgrVers = SndSoundManagerVersion();   
  56.    if ( theSndMgrVers.majorRev < 3 )
  57.       ExitToShell();
  58.  
  59.    soundInputPresent = IsSoundInputAvailable();
  60.    if ( soundInputPresent == false )
  61.       ExitToShell();
  62.       
  63.    theError = RecordSoundToMemory( &theSound );
  64.    if ( theError == userCanceledErr )
  65.       ExitToShell();
  66.  
  67.    theNewSoundID = SaveSoundFromMemoryToResource( theSound );
  68.  
  69.    ReleaseResource( theSound );
  70.    theSound = nil;
  71.    
  72.    theError = PlaySoundResourceSynch( theNewSoundID );   
  73.    if ( theError != noErr )
  74.       ExitToShell();
  75. }
  76.  
  77.  
  78. //____________________________________________________________
  79.  
  80. OSErr  RecordSoundToMemory( Handle *theSound )
  81. {
  82.    OSErr  theError;
  83.    Point  theCorner = { 50, 20 }; 
  84.    long   theTotalHeap;
  85.    long   theContigMem;
  86.    
  87.    PurgeSpace( &theTotalHeap, &theContigMem );
  88.  
  89.    *theSound = NewHandle( theContigMem - kHeapReserve );
  90.    
  91.    theError = SndRecord( nil, theCorner, siBestQuality, theSound );
  92.    
  93.    return ( theError );
  94. }
  95.  
  96.  
  97. //____________________________________________________________
  98.  
  99. long  SaveSoundFromMemoryToResource( Handle theSound )
  100. {
  101.    long   theResID;
  102.    OSErr  theError;
  103.    short  theResourceFileRef;
  104.  
  105.    theResourceFileRef = CurResFile();
  106.    
  107.    do
  108.    {
  109.       theResID = UniqueID( 'snd ' );
  110.    } while ( theResID <= kSndResIDMaxReserved );
  111.  
  112.    AddResource( theSound, 'snd ', theResID, "\pNew Sound" );
  113.    theError = ResError();
  114.    if ( theError != noErr )
  115.       ExitToShell();
  116.       
  117.    UpdateResFile( theResourceFileRef );
  118.    theError = ResError();
  119.    if ( theError != noErr )
  120.       ExitToShell();
  121.    
  122.    return ( theResID );
  123. }
  124.  
  125.  
  126. //____________________________________________________________
  127.  
  128. OSErr  PlaySoundResourceSynch( short theResID )
  129. {
  130.    Handle  theHandle;
  131.    OSErr   theError;
  132.    
  133.    theHandle = GetResource( 'snd ', theResID );
  134.    
  135.    if ( theHandle == nil )
  136.    {   
  137.       return ( resProblem );
  138.    }
  139.    else
  140.    {
  141.       HLock( theHandle );
  142.          theError = SndPlay( nil, theHandle, false );
  143.       HUnlock( theHandle );
  144.    
  145.       ReleaseResource( theHandle );
  146.  
  147.       return ( theError );
  148.    }
  149. }
  150.  
  151.  
  152. //____________________________________________________________
  153.  
  154. Boolean  IsSoundInputAvailable( void )
  155. {
  156.    OSErr    theError;
  157.    long     theResult;
  158.    Boolean  inputAvail;
  159.    
  160.    theError = Gestalt( gestaltSoundAttr, &theResult );
  161.    if ( theError != noErr )
  162.       ExitToShell();
  163.       
  164.    inputAvail = theResult & ( 1 << gestaltHasSoundInputDevice );  
  165.    if ( inputAvail > 0 )
  166.       return ( true );
  167.    else
  168.       return ( false );
  169. }
  170.  
  171.  
  172. //____________________________________________________________
  173.  
  174. void  InitializeToolbox( void )
  175. {
  176.    InitGraf( &qd.thePort );
  177.    InitFonts();
  178.    InitWindows();
  179.    InitMenus();
  180.    TEInit();
  181.    InitDialogs( 0L );
  182.    FlushEvents( everyEvent, 0 );
  183.    InitCursor();
  184. }
  185.